package com.practice.car.cardataapp.schelduer.spark.task.sale;

import com.practice.car.cardataapp.schelduer.spark.Schedule;
import com.practice.car.cardataapp.schelduer.spark.analy.SaleRunner;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static java.sql.DriverManager.getConnection;

public class TestDB implements SaleRunner {

    private static String url = Schedule.url;
    private static String user = Schedule.user;
    private static String password = Schedule.password;

    //测试代码

    @Override
    public void run(){
        //1.得到分析结果
        HashMap<String,Integer> map = MpvNationalSales.getMPVNationalSales();
        //3.把分析结果写入数据库
        insertIntoMysql(map);
    }



    //把HashMap的数据插入数据
    public static boolean insertIntoMysql(HashMap<String,Integer> hashMap){

        Connection conn = Schedule.getConnection();

        try{
            String sql = " insert into mpv_national_sales(national,sale) values (?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);

            Set<Map.Entry<String,Integer>> entry=hashMap.entrySet();
            for (Map.Entry e:entry){
                pstmt.setString(1, (String) e.getKey());
                pstmt.setInt(2, (Integer) e.getValue());
                pstmt.executeUpdate();
            }
            return true;

        } catch(SQLException e){
            e.printStackTrace();
            return false;
        }
    }

}
